﻿#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution
{
public:
    int jump(vector<int>& nums)
    {
        int left = 0, right = 0, maxPos = 0, ret = 0, n = nums.size();
        while (left <= right) // 保险的写法，以防跳不到 n - 1 的位置
        {
            if (maxPos >= n - 1) // 先判断⼀下是否已经能跳到最后⼀个位置
            {
                return ret;
            }
            // 遍历当成层，更新下⼀层的最右端点
            for (int i = left; i <= right; i++)
            {
                maxPos = max(maxPos, nums[i] + i);
            }
            left = right + 1;
            right = maxPos;
            ret++;
        }
        return -1; // 跳不到的情况
    }
};


int main()
{
    vector<int> nums = { 2,3,1,1,4 };


    cout << Solution().jump(nums) << endl;

    return 0;
}


